package com.liu.sx.util;

import java.util.Arrays;

public class BCD2String {



        /**
         * @功能: BCD码转为10进制串(阿拉伯数据)
         * @参数: BCD码
         * @结果: 10进制串
         */
        public static String bcd2Str(byte[] bytes) {
            StringBuffer temp = new StringBuffer(bytes.length * 2);
            for (int i = 0; i < bytes.length; i++) {
                temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
                System.out.println("位移1============="+((bytes[i] & 0xf0) >>> 4));
                temp.append((byte) (bytes[i] & 0x0f));
                System.out.println("位移2=============="+((bytes[i] & 0xf0)));
            }

            return temp.toString();
        }

        /**
         * @功能: 10进制串转为BCD码
         * @参数: 10进制串
         * @结果: BCD码
         */
        public static byte[] str2Bcd(String asc) {
            int len = asc.length();
            int mod = len % 2;
            if (mod != 0) {
                asc = "0" + asc;
                len = asc.length();
            }
            byte abt[] = new byte[len];
            if (len >= 2) {
                len = len / 2;
            }
            byte bbt[] = new byte[len];
            abt = asc.getBytes();
            int j, k;
            for (int p = 0; p < asc.length() / 2; p++) {
                if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                    j = abt[2 * p] - '0';
                } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                    j = abt[2 * p] - 'a' + 0x0a;
                } else {
                    j = abt[2 * p] - 'A' + 0x0a;
                }
                if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                    k = abt[2 * p + 1] - '0';
                } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                    k = abt[2 * p + 1] - 'a' + 0x0a;
                } else {
                    k = abt[2 * p + 1] - 'A' + 0x0a;
                }
                int a = (j << 4) + k;
                byte b = (byte) a;
                bbt[p] = b;
            }
            return bbt;
        }


    /**
     * 10进制转bcd
     * @param str 10进制数字 String.valueOf(int number);将10进制数字转成字符串传入此参数
     * @return bcd码
     */
    public static String DecimaltoBcd(String str){
        String b_num="";
        for (int i = 0; i < str.length(); i++) {

            String b = Integer.toBinaryString(Integer.parseInt(str.valueOf(str.charAt(i))));

            int b_len=6-b.length();

            for(int j=0;j<b_len;j++){
                b="0"+b;
            }
            b_num+=b;
        }

        return b_num;
    }


    /**
     * 16进制转bcd
     * 将16进制转成10进制，再将10进制转成bcd
     * @param hex 16进制数字String.valueOf(int number);这里忽略16进制的前缀0x，只转后面的数字为字符串类型，将16进制数字转成字符串传入此参数
     * @return bcd码
     */
    public static String HextoBcd(String hex){

        int decimal = Integer.parseInt(hex,16);

        String bcd = DecimaltoBcd(String.valueOf(decimal));

        return bcd;
    }

    public static byte[] time2BCD() {
        String time  = DateTimeUtil.getTodayChar14();
        time= time.substring(2);
        return BCD2String.str2Bcd(time);
    }


    public static byte[] day2BCD(){
        String time  = DateTimeUtil.getTodayChar8();
        System.out.println(time);
        time= time.substring(2);
        return BCD2String.str2Bcd(time);
    }


    public static void main(String[] args) {
//        byte [] address_ = {0,0,0,0,0,3};

//         00 00 00 00 00 3
//        System.out.println(bcd2Str(address_));

        byte [] address_ = {0,0,0,0,0,1};
//        00 00 00 00 012

//        00 00 00 00 099
//        00 00 00 00 00 99
//        00 00 00 00 00 01
//        00 00 00 00 00 01
        System.out.println(bcd2Str(address_));

        String  address = "000000000012";
        System.out.println(Arrays.toString(str2Bcd(address)));


        int aa =0x99;
        byte cc =(byte) 153;
        System.out.println(aa);
        System.out.println(cc);


    }






}
